package happy.diandiankong.api.resource;

import com.google.common.collect.ImmutableMap;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import happy.diandiankong.api.cache.DevicePICache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.concurrent.ExecutionException;

/**
 * Title.
 * <p>
 * Description.
 *
 * @author Bill Lv {@literal <billcc.lv@hotmail.com>}
 * @version 1.0
 * @since 2016-10-29
 */
@Path("/api/device")
@Produces(MediaType.APPLICATION_JSON)
@Api("/device")
public class DeviceResource {
    static final Logger LOGGER = LoggerFactory.getLogger(DeviceResource.class);
    static final int MIN_TP = -10;
    static final int MAX_TP = 40;

    @POST
    @Path("/tp/retrieve")
    @ApiOperation("retrieve tp")
    public Response retrieveTp() {
        final int tp;
        try {
            tp = DevicePICache.getInstance().forceGetTp();
        } catch (ExecutionException e) {
            return Response.ok(ImmutableMap.of("code", -1, "message", "error to get temperature")).build();
        }
        return Response.ok(
                ImmutableMap.of("code", 0, "message", "success", "result", ImmutableMap.of("tp", tp)))
                .build();
    }

}
